package com.example.demo.mapper;

import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    @Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userinfo")
    List<UserInfo> queryUserList();

    @Results(id = "BaseResult", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from userinfo where id = #{id}")
    List<UserInfo> queryUserInfo(Integer userId);

    @Select("select * from userinfo where id = ${id}")
    List<UserInfo> queryUserInfo2(Integer userId);

    @Select("select * from userinfo where username = #{username}")
    List<UserInfo> queryUserInfoByName(String username);

    @Select("select * from userinfo where username = '${username}'")
    List<UserInfo> queryUserInfoByName2(String username);

//    @ResultMap(value = "BaseResult")

    @Select("select * from userinfo where id = #{userId} and delete_flag = #{deleteFlag}")
    List<UserInfo> queryUserInfoByDF(@Param("userId") Integer userId, @Param("deleteFlag") Integer deleteFlag);

//    @Select("select * from userinfo where id = #{userId} and delete_flag = #{deleteFlag}")
//    List<UserInfo> queryUserInfoByDF2(@Param("id") Integer userId, @Param("deleteFlag") Integer deleteFlag);
//
//    @Select("select * from userinfo where id = #{id} and delete_flag = #{deleteFlag}")
//    List<UserInfo> queryUserInfoByDF3(@Param("id") Integer userId, @Param("deleteFlag") Integer deleteFlag);

    @Insert("insert into userinfo(username, password, age, gender, phone) " +
            "values(#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id") // 拿到自增 id
    @Insert("insert into userinfo(username, password, age, gender, phone) " +
            "values(#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")
    Integer insertByParam(@Param("userInfo") UserInfo userInfo);


    @Delete("delete from userinfo where id = #{id}")
    Integer delete(@Param("id") Integer id);

    @Update("update userinfo set password = #{password} where id = #{id}")
    Integer update(@Param("password") String password, @Param("id") Integer id);

    @Update("update userinfo set username = #{username}, password = #{password}, age = #{age} where id = #{id}")
    Integer updateByOb(UserInfo userInfo);

    @Select("select * from userinfo where username = '${username}' and password = '${password}'")
    List<UserInfo> queryUserByNameAndPassword(@Param("username") String username, @Param("password") String password);

    @Select("select * from userinfo order by id ${order}")
    List<UserInfo> queryUserByOrder(String order);

    @Select("select * from userinfo where username like concat('%', #{name}, '%')")
    List<UserInfo> queryUserByLike(String name);

//    @Select("select * from userinfo where username like #{%name%}")
//    List<UserInfo> queryUserByLike2(String name);
//
//    @Select("select * from userinfo where username like '%${name}%'")
//    List<UserInfo> queryUserByLike3(String name);

    @Delete("<script> " +
            "delete from userinfo where id in " +
            "<foreach collection='list' separator=',' open='(' close=')' item='id'> " +
            "#{id} " +
            "</foreach> " +
            "</script>")
    Integer batchDelete(List<Integer> ids);

}
